package cn.jhc.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import javax.sql.DataSource;

import cn.jhc.bean.Goods;
import cn.jhc.db.GoodsDao;
import cn.jhc.utils.Constants;

/**
 * Servlet implementation class AddGoodsServlet
 */
@WebServlet("/admin/addgoods.do")
//@MultipartConfig(location="C:/Users/Administrator/WebStore/WebContent/images") 
//@MultipartConfig(location="/home/administrator/upload/") 
//@MultipartConfig(location="/home/201421010730088/public_html/")
public class AddGoodsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddGoodsServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 request.setCharacterEncoding("UTF-8");
		DataSource dataSource = (DataSource)request.getServletContext().getAttribute(Constants.H2_DATASOURCE);	
		String goodname = request.getParameter("goodname");
	    String price = request.getParameter("price");
	    String discount = request.getParameter("discount");
	    String factory = request.getParameter("factory");
	    Part filePart = request.getPart("img");
	    String header = filePart.getHeader("Content-Disposition");
	    String filename = header.substring(header.indexOf("filename=\"") + 10, header.lastIndexOf("\""));
		 request.setAttribute("filename",filename);
//	     String imgUrl="images/"+filename;
	     
	    GoodsDao goodsDao = new GoodsDao(dataSource);
	    Goods  goods = new Goods();
	    goods.setGoodname(goodname);
	    goods.setPrice(price);
	    goods.setDiscount(discount);
	    goods.setFactory(factory);
	   
	    String imgUrl="http://infojhc.cn/~201421010730088/"+filename;
	    goods.setImg(imgUrl);
	    if(goodname!=null&&price!=null&&discount!=null&&factory!=null&&goodsDao.selectbyname(goods)==true){
	    	try {
//	    		 goodsDao.DropGoods(goods);
//	    		 goodsDao.delectAll();
				 goodsDao.addGoods(goods);
				filePart.write(filename);			
				response.sendRedirect("../list.do");
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    	
	    }else{
	    	response.sendRedirect("../other/Addfilure.jsp");
	    }
	}

}
